package com.safe.service.impl;

import com.safe.core.Constant;
import com.safe.dao.TrainDao;
import com.safe.entity.Train;
import com.safe.service.TrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TrainServiceImpl
 * Created by 涂奕恒 on 2020/7/7 16:50.
 */
@Service
public class TrainServiceImpl implements TrainService {

    @Autowired
    private TrainDao trainDao;

    @Override
    public List<Train> findAll() {
        return trainDao.findAll();
    }

    @Override
    public Page<Train> find(int pageNum, int pageSize) {
        return trainDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Train> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Train getById(int id) {
        return trainDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        trainDao.delete(id);
    }

    @Override
    public Train create(Train train) {
        return trainDao.save(train);
    }

    @Override
    public Train update(Train train) {
        return trainDao.save(train);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }
}